//
//  BiXingXunLianViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/7/22.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit


class BiXingXunLianViewController: UIViewController,BiXingSheZhiViewControllerDelegate {

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    //创建笔形训练说明,让读者一目了然
    var biXingXunLianDescription:UILabel?
    var navigationHeight:CGFloat?
    
    //定义训练时间UILabel对象
    var xunLianShiJian:UILabel?
    //定义训练分钟值UILabel对象
    var xunLianShiJianZhi:UILabel?
    //定义分钟UILabel对象
    var fenZhong:UILabel?
    
    //定义训练模式UILabel对象
    var xunLianMoShi:UILabel?
    //定义训练模式值UILabel对象
    var xunLianMoShiZhi:UILabel?
    //定义模式UILabel对象
    var moShi:UILabel?
    
    //定义选项按钮
    var xuanXiangBtn:UIButton?
    //定义开始按钮
    var kaiShiBtn:UIButton?
    
    var items:JHCustomMenu?
    
    override func viewDidLoad() {
        fengZhuangViewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.title = "笔形训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }
    
    /*
     作者:张振东
     日期:20176-09-24
     功能:封装viewDidLoad函数中的初始化label对象
     */
    func fengZhuangUiLabelAttributes(_ label:UILabel){
        switch label {
        case biXingXunLianDescription!:
            //初始化小键盘训练说明
            label.layer.cornerRadius = 10
            label.clipsToBounds = true
            label.backgroundColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1)
            label.text = "在笔形训练中,笔形用红色标出,您共有两次输入机会,若首次输入正确,则生成下一个笔形,否则输入框将变为红色以示警告,再次输入时不管正确与否都将生成下一个笔形.点击背景图片,您可以查看该笔形对应的纵横码."
            label.font = UIFont.systemFont(ofSize: 17)
            label.layer.borderWidth = 1
            label.layer.borderColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1).cgColor
            label.numberOfLines = 0
            self.automaticallyAdjustsScrollViewInsets = false
        case xunLianShiJian!:
            label.text = "训练时间:"
            label.font = UIFont.systemFont(ofSize: 16)
            
        case xunLianShiJianZhi!:
            
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "1"
        case fenZhong!:
            
            label.text = "分钟"
            label.font = UIFont.systemFont(ofSize: 16)
        case xunLianMoShi!:
            
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "训练模式:"
        case xunLianMoShiZhi!:
            
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "计时"
        case moShi!:
            
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "模式"
        default:
            print("初始化完毕")
        }
        
    }
    
    /*
     作者:张振东
     日期:20176-09-24
     功能:封装viewDidLoad函数中的初始化label对象
     */
    func fengZhuangViewDidLoad(){
        self.title = "笔形训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //设置右边的按钮
//        let rightBarButton = UIButton(frame:CGRectMake(screenWidth-20,0,20,20))
//        rightBarButton.setBackgroundImage(UIImage(named: "sangeheng"), forState: .Normal)
//        let rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
//        rightBarButton.addTarget(self, action: #selector(jiaZaiTiXingLieBiao), forControlEvents: .TouchUpInside)
//        
//        self.navigationItem.rightBarButtonItems = [rightBarButtonItem]
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height

        //初始化小键盘训练说明
        biXingXunLianDescription = UILabel(frame:CGRect(x: 10,y: navigationHeight!+70,width: screenWidth-20,height: 130))
        fengZhuangUiLabelAttributes(biXingXunLianDescription!)
        //初始化训练时间UILabel对象
        xunLianShiJian = UILabel(frame:CGRect(x: screenWidth/2-70,y: navigationHeight!+70+100+50,width: 70,height: 25))
        fengZhuangUiLabelAttributes(xunLianShiJian!)
        
        //初始化训练时间值UILabel对象
        xunLianShiJianZhi = UILabel(frame:CGRect(x: screenWidth/2+15,y: navigationHeight!+70+100+50,width: 30,height: 25))
        fengZhuangUiLabelAttributes(xunLianShiJianZhi!)
        
        //初始化分钟UILabel对象
        fenZhong = UILabel(frame:CGRect(x: screenWidth/2+50,y: navigationHeight!+70+100+50,width: 40,height: 25))
        fengZhuangUiLabelAttributes(fenZhong!)

        
        //初始化训练模式UIlabel对象
        xunLianMoShi = UILabel(frame:CGRect(x: screenWidth/2-70,y: navigationHeight!+70+100+50+25+20,width: 70,height: 25))
        fengZhuangUiLabelAttributes(xunLianMoShi!)
        
        //初始化训练模式值UILabel对象
        xunLianMoShiZhi = UILabel(frame:CGRect(x: screenWidth/2+10,y: navigationHeight!+70+100+50+25+20,width: 40,height: 25))
        fengZhuangUiLabelAttributes(xunLianMoShiZhi!)
        
        //初始化训练模式值UILabel对象
        moShi = UILabel(frame:CGRect(x: screenWidth/2+50,y: navigationHeight!+70+100+50+25+20,width: 40,height: 25))
        fengZhuangUiLabelAttributes(moShi!)
        
        //初始化我知道了按钮
        kaiShiBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-60,width: screenWidth-20,height: 40))
        kaiShiBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        kaiShiBtn?.setTitle("开始", for: UIControlState())
        kaiShiBtn?.layer.cornerRadius = 10
        kaiShiBtn?.setTitleColor(UIColor.white, for: UIControlState())
        
        //初始化选项按钮
        xuanXiangBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-110,width: screenWidth-20,height: 40))
        xuanXiangBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        xuanXiangBtn?.setTitle("选项", for: UIControlState())
        xuanXiangBtn?.layer.cornerRadius = 10
        xuanXiangBtn?.setTitleColor(UIColor.white, for: UIControlState())
        
        self.view.addSubview(biXingXunLianDescription!)
        self.view.addSubview(xunLianShiJian!)
        self.view.addSubview(xunLianShiJianZhi!)
        self.view.addSubview(fenZhong!)
        self.view.addSubview(xunLianMoShi!)
        self.view.addSubview(xunLianMoShiZhi!)
        self.view.addSubview(moShi!)
        self.view.addSubview(xuanXiangBtn!)
        self.view.addSubview(kaiShiBtn!)

        //给按钮"我知道了"添加点击事件
        kaiShiBtn?.addTarget(self, action: #selector(tiaoZhuanZhiKaiShiXunLianViewController), for: .touchUpInside)
        xuanXiangBtn?.addTarget(self, action: #selector(tiaoZhuanZhiXuanXiangViewController), for: .touchUpInside)
        super.viewDidLoad()
    }
    
    func chuanDiBiXingMoShiAndFenZhong(_ xuanZhongDeMoShiZhi: String, xuanZhongFenZhongZhi: Int) {
        print(xuanZhongDeMoShiZhi)
        print(xuanZhongFenZhongZhi)
        self.xunLianMoShiZhi?.text = xuanZhongDeMoShiZhi
        self.xunLianShiJianZhi?.text = "\(xuanZhongFenZhongZhi)"
    }

    /*
        作者:张振东
        日期:2017-09-24
        功能:跳转至选项ViewController
     */
    func tiaoZhuanZhiXuanXiangViewController(){
        let biXingXuanXiangViewController = BiXingXuanXiangViewController()
        biXingXuanXiangViewController.delegate = self
        self.navigationController?.pushViewController(biXingXuanXiangViewController, animated:true)
    }
    
    /*
        作者:张振东
        日期:20176-09-24
        功能:跳转至开始练习ViewController
     */
    func tiaoZhuanZhiKaiShiXunLianViewController(){
        let biXingExersizeMainViewController = BiXingExersizeMainViewController()
        biXingExersizeMainViewController.moShiLeiBie = self.xunLianMoShiZhi?.text
        biXingExersizeMainViewController.fenZhongZhi = self.xunLianShiJianZhi?.text
        self.navigationController?.pushViewController(biXingExersizeMainViewController, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
     作者:张振东
     日期:2016-10-20
     功能:加载列表
     */
//    func jiaZaiTiXingLieBiao(){
//        print("111")
//        if (self.items == nil){
//            self.items = JHCustomMenu.init(dataArr: ["单选题","判断题","填空题"], origin: CGPointMake(screenWidth-140,55), width: 125, rowHeight: 44)
//            items?.delegate = self
//            items?.dismiss = {
//                self.items = nil
//            }
//            items?.arrImgName = ["item_school.png","item_battle.png","item_share.png"]
//            self.view.addSubview(items!)
//        }else{
//            items?.dismissWithCompletion({ (object) in
//                self.items = nil
//            })
//        }
//    }
    
    /*
        作者:张振东
        日期:2016-10-20
        功能:实现JHCustomDelegate代理
     */
//    func jhCustomMenu(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
//        print(indexPath.row)
//        switch indexPath.row {
//        case 0:
//            let biXingDanXuanTiViewController = BiXingDanXuanTiViewController()
//            self.navigationController?.pushViewController(biXingDanXuanTiViewController, animated: true)
//            break
//        case 1:
//            let biXingPanDuanTiViewController = BiXingPanDuanTiViewController()
//            self.navigationController?.pushViewController(biXingPanDuanTiViewController, animated: true)
//            break
//
//        case 2:
//            let biXingTianKongTiViewController = BiXingTianKongTiViewController()
//            self.navigationController?.pushViewController(biXingTianKongTiViewController, animated: true)
//            break
//        default:
//            print("单选题")
//        }
//    }
    
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
